﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PocketEngine.Maths;
using PocketEngine.Engine;
using Tao.OpenGl;
using PocketEngine.Geometry;
namespace PocketEngine.Gui
{
    public class pWidget
    {
        public int X
        {
            get
            {
                return mX;
            }
            set
            {
                mX = value;
            }
        }
        public void OnKeyDown(int key)
        {
        }
        public void OnKeyUp(int key)
        {
        }

        public bool InBounds(int x, int y)
        {
            if (x >= WorldX && x <= WorldX + Width)
            {
                if (y >= WorldY && y <= WorldY + Height)
                {
                    return true;
                }
            }
            return false;
        }
        public pTriangles Body = null;
        private int mX = 0;
        public int Y
        {
            get
            {
                return mY;
            }
            set
            {
                mY = value;
            }
        }
        private int mY = 0;

        public int WorldX
        {
            get
            {
                int x = 0;
                if (Parent != null)
                {
                    x = Parent.WorldX;
                }
                return x + X;
            }
        }
        public int WorldY
        {
            get
            {
                int y = 0;
                if (Parent != null)
                {
                    y = Parent.WorldY;
                }
                return y + Y;
            }
        }

        public int Width
        {
            get
            {
                return mWidth;
            }
            set
            {
                mWidth = value;
            }
        }
        private int mWidth = 0;
        public int Height
        {
            get
            {
                return mHeight;
            }
            set
            {
                mHeight = value;
            }
        }
        private int mHeight = 0;


        public void Set(int x, int y, int w, int h, pWidget parent)
        {
            X = x;
            Y = y;
            Width = w;
            Height = h;
            if (parent != null)
            {
                parent.Add(this);
            }
        }

        public void Add(pWidget widget)
        {
            Widgets.Add(widget);
            widget.Parent = this;
        }
        public float Alpha
        {
            get
            {

                return mAlpha;
            }
            set
            {
                mAlpha = value;
            }
        }
        private float mAlpha = 0;

        public float AmbientAlpha
        {
            get
            {
                float wa = 1;
                if (Parent != null)
                {
                    wa = Parent.AmbientAlpha;

                }
                return Alpha * wa;
            }
        }

        public pWidget Parent
        {
            get
            {
                return mParent;
            }
            set
            {
                mParent = value;
            }
        }
        private pWidget mParent = null;
        public List<pWidget> Widgets = new List<pWidget>();

        public virtual void OnMouseMove(pVec2 pos)
        {
        }
        public virtual void OnMouseDown(int button)
        {
        }
        public virtual void OnMouseUp(int button)
        {
        }
        public virtual void OnMouseEnter()
        {
        }
        public virtual void OnMouseLeave()
        {
        }
        public virtual void OnActivate()
        {
        }
        public virtual void OnDeactivate()
        {
        }
        public void UpdateWidget()
        {
            Update();
            foreach (var w in Widgets)
            {
                w.UpdateWidget();
            }
        }
        public virtual void Update()
        {

        }
        public void DrawWidget()
        {
            Draw(WorldX,WorldY);
            foreach (var w in Widgets)
            {
                w.DrawWidget();
            }
        }
        public virtual void Draw(int dx, int dy)
        {

        }
        private void SetAlpha()
        {
            Gl.glEnable(Gl.GL_BLEND);
            Gl.glBlendFunc(Gl.GL_SRC_ALPHA,Gl.GL_ONE_MINUS_SRC_ALPHA);

        }
        public virtual void _Frame(int x, int y, int w, int h, Engine.pColor top, Engine.pColor bot)
        {
            SetAlpha();
            _Rect(x, y, w, (int)(h * 0.25f), top, bot);
            _Rect(x, y + (int)(h * 0.25f), w, (int)(h * 0.75f), bot, top);
            _Line(x, y, x + w, y, top, bot);
            _Line(x, y, x, y + h, bot, top);
            _Line(x+w, y, x + w, y + h, top, bot);
            _Line(x, y + h, x + w, y + h, top, bot);
        }
        public virtual void _Line(int x, int y, int x2, int y2, pColor top, pColor bot)
        {
            Gl.glBegin(Gl.GL_LINES);
            top.Bind();
            Gl.glVertex2f(x, y);
            bot.Bind();
            Gl.glVertex2f(x2, y2);
            Gl.glEnd();

        }
        public virtual void _Rect(int x,int y,int w,int h,Engine.pColor top,Engine.pColor bot)
        {
            
            SetAlpha();       
            top.Bind();
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glTexCoord2f(0,1);
            Gl.glVertex2f(x,y);
            Gl.glTexCoord2f(1,1);
            Gl.glVertex2f(x+w,y);
            bot.Bind();
            Gl.glTexCoord2f(1,0);
            Gl.glVertex2f(x+w,y+h);
            Gl.glTexCoord2f(0,0);
            Gl.glVertex2f(x,y+h);
            Gl.glEnd();
        }
    }
}
